import 'package:core_package/core_package.dart';
import 'package:flutter/material.dart';

enum TipPositionEnum {
  left,
  middle,
  right,
}

/// 通用气泡提示组件
class CommonPositionTip extends StatefulWidget {
  /// 控件透明度，解决和其他文字重叠看不清问题
  final double? opacity;

  /// 箭头朝上,默认true
  final bool isTop;

  /// 箭头显示位置
  final TipPositionEnum position;

  /// 提示文字
  final String? tips;

  final TextStyle? textStyle;

  final StrutStyle? strutStyle;

  /// 气泡点击事件
  final Function()? onTipClick;

  /// 点击关闭后需处理的业务
  final Function? onClose;
  final double? radius;
  final double leftPoint;

  ///是否需要关闭按钮
  final bool needCloseClick;

  const CommonPositionTip(
    this.tips, {
    Key? key,
    this.isTop = true,
    this.onTipClick,
    this.textStyle,
    this.strutStyle,
    this.position = TipPositionEnum.left,
    this.onClose,
    this.opacity,
    this.radius,
    this.leftPoint = 0,
    this.needCloseClick = true,
  })  : assert(tips != null),
        super(key: key);

  @override
  _CommonPositionTipState createState() => _CommonPositionTipState();
}

class _CommonPositionTipState extends State<CommonPositionTip> {
  bool _isShow = true;

  @override
  Widget build(BuildContext context) {
    if (_isShow)
      return GestureDetector(
        onTap: widget.onTipClick,
        child: Column(
          crossAxisAlignment: widget.position == TipPositionEnum.left
              ? CrossAxisAlignment.start
              : (widget.position == TipPositionEnum.right ? CrossAxisAlignment.end : CrossAxisAlignment.center),
          children: <Widget>[
            if (widget.isTop) _setPositionTip(),
            Opacity(
              opacity: widget.opacity ?? 0.7,
              child: Container(
                decoration: BoxDecoration(
                    color: Color(0xff2E2E2E),
                    borderRadius: BorderRadius.circular(widget.radius ?? 18.px),
                    boxShadow: [
                      BoxShadow(
                          color: Color(0xff5F5F5F),
                          offset: Offset(2.0, 2.0), //阴影xy轴偏移量
                          blurRadius: 4.0, //阴影模糊程度
                          spreadRadius: 0.0 //阴影扩散程度
                          )
                    ]),
                padding: EdgeInsets.only(
                    left: widget.needCloseClick ? 14.px : 10.px, right: 10.px, top: (10.px), bottom: (10.px)),
                child: Row(
                  children: <Widget>[
                    Text(widget.tips??'',
                        strutStyle: widget.strutStyle,
                        style: widget.textStyle ??
                            TextStyle(
                              color: Color(0xffffffff),
                              fontSize: (12.px),
                              fontWeight: FontWeight.w700,
                              fontStyle: FontStyle.normal,
                            )),
                    if (widget.needCloseClick)
                      SizedBox(
                        width: 2.px,
                      ),
                    if (widget.needCloseClick)
                      InkWell(
                        onTap: () {
                          if (mounted) {
                            setState(() {
                              _isShow = false;
                            });
                          }
                          widget.onClose?.call();
                        },
                        child: Icon(
                          Icons.close,
                          color: Color(0xffD8D8D8),
                          size: 14.px,
                        ),
                      )
                  ],
                ),
              ),
            ),
            if (!widget.isTop) _setPositionTip()
          ],
        ),
      );
    return Container();
  }

  Widget _setPositionTip() {
    return Container(
      padding: widget.position == TipPositionEnum.left
          ? EdgeInsets.only(left: 18.px + widget.leftPoint)
          : (widget.position == TipPositionEnum.right
              ? EdgeInsets.only(right: 18.px + widget.leftPoint)
              : EdgeInsets.all(
                  0,
                )),
      child: Image.asset(
        MyAssetsImages.setAssetsImagesPath(_setImage()),
        width: 10.px,
        height: (widget.position == TipPositionEnum.middle ? 7.px : 6.px),
      ),
    );
  }

  /// 添加图时要严格按照此方法规则命名
  ///
  /// cece_position_tip_icon
  ///
  /// [widget.isTop] "top",反之 "under"
  ///
  /// [widget.position] "left" "right" "middle"
  String _setImage() {
    return "cece_position_tip_icon_${widget.isTop ? "top" : "under"}_${widget.position == TipPositionEnum.left ? "left" : (widget.position == TipPositionEnum.right ? "right" : "middle")}.png";
  }
}
